class Solution {
public:
    int longestPalindrome(vector<string>& words) {
        int ret=0;
        unordered_map<string,int> has;
        unordered_map<string,int> same;
        int cnt=0;
        for(int i=0;i<words.size();i++)
        {
            if(words[i][0]==words[i][1])
            {
                same[words[i]]++;
                if(same[words[i]]==2)
                {
                    ret+=4;
                    same[words[i]]-=2;
                    if(same[words[i]]==0)
                        same.erase(words[i]);
                }
            }
            else{
                has[words[i]]++;
                string t=words[i];
                reverse(t.begin(),t.end());
                if(has.count(t))
                {
                    ret+=4;
                    has[t]--;
                    has[words[i]]--;
                    if(has[t]==0)
                        has.erase(t);
                    if(has[words[i]]==0)
                        has.erase(words[i]);

                }

            }
        }
        if(same.size())
            ret+=2;

        return ret;
    }
};
